package cn.edu.xmut.chen.zhlg.zhlg.user.controller;

import cn.edu.xmut.chen.zhlg.common.HttpResult.Result;
import cn.edu.xmut.chen.zhlg.zhlg.user.entity.User;
import cn.edu.xmut.chen.zhlg.zhlg.user.param.UserLoginParam;
import cn.edu.xmut.chen.zhlg.zhlg.user.param.UserRegisterParam;
import cn.edu.xmut.chen.zhlg.zhlg.user.param.UserUpdateParam;
import cn.edu.xmut.chen.zhlg.zhlg.user.result.UserInfoResult;
import cn.edu.xmut.chen.zhlg.zhlg.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Api("用户管理 API")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Resource
    private UserService userService;

    @RequestMapping(method = RequestMethod.POST, value = "/register")
    @ApiOperation(value = "添加新用户", notes = "此方法用于向数据库添加一位新用户")
    @ApiImplicitParam(name = "user", value = "需要添加的用户对象", required = true, dataType = "User")
    public Result register(@RequestBody UserRegisterParam userRegisterParam) {
        return Result.success("注册成功").data(userService.register(userRegisterParam));
    }

    @RequestMapping(method = RequestMethod.POST, value = "/login")
    @ApiOperation(value = "登录", notes = "此方法用于登录")
    @ApiImplicitParam(name = "user", value = "需要登录的用户对象", required = true, dataType = "User")
    public Result Login(@RequestBody UserLoginParam userLoginParam ) {
        return Result.success("登录成功").data(userService.login(userLoginParam));
    }

    @RequestMapping(method = RequestMethod.GET, value = "/detail")
    @ApiOperation(value = "通过token解析获取用户信息", notes = "此方法用于token转化为信息")
    @ApiImplicitParam(name = "user", value = "登录的用户对象", required = true, dataType = "User")
    public Result detail(String token) {
        return Result.success("获取成功").data(userService.getDetail(token));
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPersonalDetail")
    @ApiOperation(value = "获取用户个人信息", notes = "此方法用于获取用户个人信息")
    @ApiImplicitParam(name = "user", value = "登录的用户对象", required = true, dataType = "User")
    public Result getPersonalDetail() {
        return Result.success("获取成功").data(userService.getPersonalDetail());
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getPersonalDetailById")
    @ApiOperation(value = "获取用户个人信息", notes = "此方法用于通过用户id获取用户个人信息")
    @ApiImplicitParam(name = "user", value = "登录的用户对象", required = true, dataType = "User")
    public Result getPersonalDetailById(String userId) {
        return Result.success("获取成功").data(userService.getDetailByUserId(userId));
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/info")
    public Result getUserInfo(@RequestParam String userId) {
        UserInfoResult userInfo = userService.getUserInfo(userId);
        if (userInfo == null) {
            return Result.error("用户不存在");
        }
        return Result.success(userInfo);
    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    public Result updateUserInfo(@RequestBody @Validated UserUpdateParam param) {
        boolean success = userService.updateUserInfo(param.getId(), param);
        if (!success) {
            return Result.error("更新失败");
        }
        return Result.success(true);
    }
}
